package com.ys.controller.web.bi;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.ys.service.bi.BiBusinessFundsService;
import com.ys.service.bi.BiClassifyModuleService;
import com.ys.service.bi.BiFlowAnalysisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSONObject;
import com.ys.mapper.bi.BiMapper;
import com.ys.mapper.sys.SysMapper;
import com.ys.service.sys.ImgcodeService;
import com.ys.util.DateUtil;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.R;

import lombok.AllArgsConstructor;
import lombok.Data;

@Controller
@RequestMapping("/web/bi/business")
public class WebBiBusinessController {
	private static final Logger logger = LoggerFactory.getLogger(WebBiBusinessController.class);

	public static Long MENU_ID = 10148L;

	@Autowired
	private ImgcodeService imgcodeService;
	@Autowired
	private BiMapper biMapper;
	@Resource
	private SysMapper sysMapper;

	@Autowired
	private BiClassifyModuleService biClassifyModuleService;

	@Autowired
	private BiBusinessFundsService biBusinessFundsService;

	@Autowired
	private BiFlowAnalysisService biFlowAnalysisService;

	/*
	 * 业务资金
	 */
	@GetMapping("/fund")
	public String fund(Model model, String loginName, String pass){
		return "bi/business/fund";
	}

	/*
	 * 业务流水
	 */
	@GetMapping("/running")
	public String running(Model model, String loginName, String pass){
		return "bi/business/running";
	}

	/*
	 * 分类
	 */
	@GetMapping("/category")
	public String trend(Model model, String loginName, String pass){
		return "bi/business/category";
	}


	/**
	 * bi页面获取数据，根据类型
	 */
	@PostMapping("/getInfoByClassifyType")
	@ResponseBody
	public JSONObject getInfoByClassifyType(HttpServletRequest request, @RequestParam Map<String, Object> params){
		JSONObject res = new JSONObject();
		try {
			int classifyType = Integer.parseInt(params.get("classifyType").toString());
			res = biClassifyModuleService.getInfoByClassifyType(classifyType);
			res.put("code",R.Code.SUCCESS.value);
		}catch (Exception e){
			logger.error("获取分类模块统计数据出错：",e.getMessage(),e);
			res.put("code",R.Code.FAILED.value);
			res.put("msg","获取数据失败，请稍后重试！");
		}
		return res;
	}


	/**
	 * 业务资金
	 */
	@PostMapping("/getBusinessFunds")
	@ResponseBody
	public JSONObject getBusinessFunds(){
		JSONObject res = new JSONObject();
		try {
			res = biBusinessFundsService.getBusinessFunds();
			res.put("code",R.Code.SUCCESS.value);
		}catch (Exception e){
			logger.error("获取业务资金BI统计数据出错：",e.getMessage(),e);
			res.put("code",R.Code.FAILED.value);
			res.put("msg","获取业务资金BI数据失败，请稍后重试！");

		}
		return res;
	}

	/**
	 * bi流水分析页面获取数据，根据类型 dateType
	 */
	@PostMapping("/getInfoByFlowAnalysis")
	@ResponseBody
	public JSONObject getInfoByFlowAnalysis(HttpServletRequest request, @RequestParam Map<String, Object> params){
		JSONObject res = new JSONObject();
		try {
			int dateType = Integer.parseInt(params.get("dateType").toString());
			res = biFlowAnalysisService.getInfoByDateType(dateType);
			res.put("code",R.Code.SUCCESS.value);
		}catch (Exception e){
			logger.error("获取流水分析统计数据出错：",e.getMessage(),e);
			res.put("code",R.Code.FAILED.value);
			res.put("msg","获取数据失败，请稍后重试！");
		}
		return res;
	}
}
